package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.base.Contants;
import com.zx.kitchen.index.common.http.HttpIndex;
import com.zx.kitchen.index.common.mvp.OnResultListener;
import com.zx.kitchen.index.common.utils.PreferenceUtil;
import com.zx.kitchen.index.common.utils.Toasts;
import com.zx.kitchen.index.common.view.ClearEditText;
import com.zx.kitchen.index.common.view.VerifyCode;

public class LoginActivity extends Activity implements View.OnClickListener, OnResultListener {

    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private TextView tvChangeVerifyCode;
    private TextView forgetPassword;
    private Button btnLogin;
    private ClearEditText etLoginName;
    private ClearEditText etLoginPassword;
    private EditText etVerificationCode;
    private CheckBox rememberPassword;
    private VerifyCode verifyCode;
    private String etLoginNameStr;
    private String etLoginPasswordStr;
    private String etVerificationCodeStr;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        this.context = getApplicationContext();
        initView();
        initListener();
    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        tvChangeVerifyCode = (TextView) findViewById(R.id.tv_change_verify_code);
        forgetPassword = (TextView) findViewById(R.id.forget_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        etLoginName = (ClearEditText) findViewById(R.id.et_login_name);
        etLoginPassword = (ClearEditText) findViewById(R.id.et_login_password);
        etVerificationCode = (EditText) findViewById(R.id.et_verification_code);
        rememberPassword = (CheckBox) findViewById(R.id.remember_password);
        verifyCode = (VerifyCode) findViewById(R.id.verify_code);
        toolbarTitle.setText(getResources().getString(R.string.Login));
        toolbarTitle.setTextColor(getResources().getColor(R.color.orange));

        etLoginNameStr = PreferenceUtil.getStringData(context, Contants.spUserName);
        etLoginPasswordStr = PreferenceUtil.getStringData(context, Contants.spPassWord);
        if (!TextUtils.isEmpty(etLoginNameStr)) {
            etLoginName.setText(etLoginNameStr);
        }
        if (!TextUtils.isEmpty(etLoginPasswordStr)) {
            etLoginPassword.setText(etLoginPasswordStr);
        }
        if (PreferenceUtil.getBooleanData(context, Contants.IS_LOGIN)){
            startActivity(new Intent(LoginActivity.this, IndexActivity.class));
        }
    }

    private void initListener() {
        btnLogin.setOnClickListener(this);
        toolbarLeft.setOnClickListener(this);
        tvChangeVerifyCode.setOnClickListener(this);
        rememberPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {

                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_login:
                etLoginNameStr = etLoginName.getText().toString();
                etLoginPasswordStr = etLoginPassword.getText().toString();
                etVerificationCodeStr = etVerificationCode.getText().toString();

                if (rememberPassword.isChecked()) {
                    PreferenceUtil.saveData(context, Contants.spUserName, etLoginNameStr);
                    PreferenceUtil.saveData(context, Contants.spPassWord, etLoginPasswordStr);
                }


                if (verifyCode != null && !TextUtils.isEmpty(etVerificationCodeStr)) {
                    if ((verifyCode.isEqualsIgnoreCase(etVerificationCodeStr))) {
                        //  账号：test_gys_run   密码：admin123
//                        HttpIndex.getInstanse().getLogin(1, context, etLoginNameStr, etLoginPasswordStr, new OnResultListener() {
                        HttpIndex.getLogin(context, "test_gys_run", "admin123", this, null);
                    } else {
                        Toasts.show(context, "校验码错误");
                    }
                }


                break;
            case R.id.tv_change_verify_code:
                verifyCode.refresh();
                break;
            case R.id.toolbar_leftLin:
                finish();
                break;
        }
    }



    @Override
    public void onSuccess(int requestCode, String str) {
        Log.d("LoginActivity===", str);
        PreferenceUtil.saveData(context, Contants.spToken, str);
        if (!TextUtils.isEmpty(str)){
            PreferenceUtil.saveData(context, Contants.IS_LOGIN, true);
        }
        startActivity(new Intent(LoginActivity.this, IndexActivity.class));
    }


    @Override
    public void onFailure(int requestCode, String str) {
        Toasts.show(context, "登录失败");

    }
}
